# 餐厅发现器API测试脚本
# 测试高德地图API集成和搜索功能

Write-Host "🧪 餐厅发现器API测试" -ForegroundColor Green
Write-Host "================================" -ForegroundColor Green

$baseUrl = "http://localhost:8080"

try {
    # 1. 健康检查
    Write-Host "🏥 健康检查..." -ForegroundColor Yellow
    $health = Invoke-RestMethod -Uri "$baseUrl/health" -TimeoutSec 5
    Write-Host "✅ 健康状态: $($health.status)" -ForegroundColor Green

    # 2. 系统诊断
    Write-Host "🔧 系统诊断..." -ForegroundColor Yellow
    $diagnostics = Invoke-RestMethod -Uri "$baseUrl/api/diagnostics" -TimeoutSec 5
    Write-Host "📋 诊断结果:" -ForegroundColor Cyan
    Write-Host "   环境: $($diagnostics.environment)" -ForegroundColor White
    Write-Host "   高德地图状态: $(if ($diagnostics.gaodeMap.enabled) { '启用' } else { '禁用' })" -ForegroundColor White
    Write-Host "   API密钥: $(if ($diagnostics.gaodeMap.hasApiKey) { '已配置' } else { '未配置' }) (长度: $($diagnostics.gaodeMap.apiKeyLength))" -ForegroundColor White
    Write-Host "   系统状态: $($diagnostics.status)" -ForegroundColor White

    # 3. 模拟搜索请求（通过访问主页触发）
    Write-Host "🔍 测试页面访问..." -ForegroundColor Yellow
    try {
        $response = Invoke-WebRequest -Uri $baseUrl -TimeoutSec 10
        if ($response.StatusCode -eq 200) {
            Write-Host "✅ 主页访问成功" -ForegroundColor Green
            
            # 检查页面内容
            if ($response.Content -match "餐厅发现器") {
                Write-Host "✅ 页面内容正常" -ForegroundColor Green
            } else {
                Write-Host "⚠️ 页面内容可能异常" -ForegroundColor Yellow
            }
        }
    } catch {
        Write-Host "❌ 页面访问失败: $($_.Exception.Message)" -ForegroundColor Red
    }

    Write-Host "" -ForegroundColor White
    Write-Host "🎉 测试完成！" -ForegroundColor Green
    Write-Host "🌐 访问应用: $baseUrl" -ForegroundColor Cyan
    Write-Host "📋 诊断页面: $baseUrl/api/diagnostics" -ForegroundColor Cyan
    
} catch {
    Write-Host "❌ 测试失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "🔍 请检查应用是否正在运行" -ForegroundColor Yellow
    exit 1
} 